package com.example.e

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.e.Adapter.ShopAdapter
import com.example.e.Bean.Shop
import com.example.e.HttpUse.HttpUtil
import com.example.e.HttpUse.IP
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.activity_search_result.*
import kotlinx.android.synthetic.main.fragment_home.*
import okhttp3.*
import org.json.JSONArray
import java.io.IOException
import java.lang.StringBuilder
import java.net.URLEncoder
import kotlin.concurrent.thread

class SearchResultActivity : AppCompatActivity() {

    val updateText = 1
    val search_shop = "${IP.ServerIP}e/shop/search_shop_json.jsp"
    private val shopsearchList: ArrayList<Shop> = java.util.ArrayList<Shop>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_result)

        //布局管理器
        val layoutManager = LinearLayoutManager(this)
        recyclerviewShopResult.layoutManager = layoutManager
        val adapter = ShopAdapter(shopsearchList)

        val name = intent.getStringExtra("name")
        getShopJson(adapter,name.toString())

        iv_arrow2.setOnClickListener(){
            val intent = Intent(this,SearchActivity::class.java)
            startActivity(intent)
        }

    }
    private fun getShopJson(adapter: ShopAdapter,name:String) {
        thread {
            try {
                val client = OkHttpClient()

                val requestBody = FormBody.Builder()
                    .add("name", URLEncoder.encode(name,"UTF-8"))
                    .build()

                val request = Request.Builder()
                    .url(search_shop)
                    .post(requestBody)
                    .build()
                //执行
                val response = client.newCall(request).execute()
                //得到返回值
                val responseData = response.body!!.string()
                val result = parseJson(responseData)
                if(responseData != null){
                    runOnUiThread {
                        //     tvResponse.text = result
                        recyclerviewShopResult.adapter = adapter
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
            }

        }
    }
    private fun parseJson(jsonStr:String): java.util.ArrayList<Shop> {
        shopsearchList.clear()
        val builder = StringBuilder()
        try {
            //json数组
            val jsonArray = JSONArray(jsonStr)
            for(i in 0 until jsonArray.length()){
                //元素取出
                val jsonObject = jsonArray.getJSONObject(i)
                val id = jsonObject.getInt("id")
                val name = jsonObject.getString("name")
                val imageURL = jsonObject.getString("imageURL")
                val score = jsonObject.getString("score")
                val sale = jsonObject.getString("sale")
                val sprice = jsonObject.getString("sprice")
                val dprice = jsonObject.getString("dprice")
                val distance = jsonObject.getString("distance")
                val time = jsonObject.getString("time")
                shopsearchList.add(Shop(name,imageURL,score,sale,sprice,dprice,distance,time))
                //Toast.makeText(this,"${imageURL}", Toast.LENGTH_LONG).show()
                builder.append("$name--$imageURL--")
            }
        }catch (e: Exception){
            e.printStackTrace()
        }
        //Toast.makeText(this,"${builder}",Toast.LENGTH_LONG).show()
        return shopsearchList
    }
}